package reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reggie.common.R;
import reggie.entity.Category;
import reggie.entity.Dish;
import reggie.entity.Setmeal;
import reggie.entity.SetmealDto;
import reggie.service.CategoryService;
import reggie.service.DishService;
import reggie.service.SetmealDishService;
import reggie.service.SetmealService;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ClassName: SetmealController <br/>
 * Description: <br/>
 * date: 2022/12/3 10:28<br/>
 *
 * @author Xingyingsheng<br />
 * @since JDK 11
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name != null, Setmeal::getName, name).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo);

        Page<SetmealDto> dtoPage = new Page<>();
//        copy
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<SetmealDto> list = pageInfo.getRecords().stream().map(item -> {

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;

        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
    @PostMapping("/status/{status}")
    public R<String> haltSales(@PathVariable(value = "status")int status,@RequestParam(value = "ids") List<Long> ids){
        log.info("启停售信息：{} ，{}",status,ids.toString());
        List<Setmeal> list = setmealService.listByIds(ids);
        List<Setmeal> collect = list.stream().map(item -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(collect);
        String s = status==1 ? "启售成功":"停售成功";
        return R.success("s");
    }




    @GetMapping("/{id}")
    private R<Setmeal> getSetmealById(@PathVariable(value = "id") Long setmealId) {
        log.info("套餐信息修改内容回显：{}", setmealId);
        Setmeal setmeal = setmealService.getById(setmealId);
        return R.success(setmeal);
    }


    @DeleteMapping
    public R<String> delete(@RequestParam List< Long> ids){
            setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}


